#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################

rm(list=ls())

#'#
#'# Chemins de travail
#'#
R_ROOT <- "C:/UQAR/Recherche/Maitrise/Donnees"
R_WORKING_DIRECTORY <- file.path(R_ROOT, "R")


#'#
#'# Chargement des dependances
#'# 
source(file.path(R_WORKING_DIRECTORY, "Utils/loadDependencies.R"))


grid.newpage()
t <- grid.table(head(iris), h.even.alpha=1, h.odd.alpha=1, 
                       v.even.alpha=0.5, v.odd.alpha=1,
                   show.rownames=FALSE, rows=NULL,
                   show.namesep=TRUE,
                   gpar.colfill= gpar(col=c("red", "blue","red", "blue")),
                   show.hlines=TRUE, separator="black", name="test")

               
 data(iris)
 par(mfrow=c(2,2))
 plot( Sepal.Length ~ Species, data=iris, border="blue", col="cyan",
 main="Boxplot of Sepal Length by Species" )
 plotmeans( Sepal.Length ~ Species, data=iris, barwidth=2, connect=FALSE,
     main="Means and 95% Confidence Intervals\nof Sepal Length by Species")
 
 info <- sapply( split(iris$Sepal.Length, iris$Species),
     function(x) round(c(Mean=mean(x), SD=sd(x), N=gdata::nobs(x)),2) )
 
 textplot( info, valign="top"  )
 title("Sepal Length by Species")

 reg <- lm( Sepal.Length ~ Species, data=iris )
 textplot( capture.output(summary(reg)), valign="top")
 title("Regression of Sepal Length by Species")
 
par(mfrow=c(1,1))
 
### Show how to control text color
cols <- c("red", "green", "magenta", "forestgreen")
mat <- cbind(name=cols, t(col2rgb(cols)), hex=col2hex(cols))

textplot(mat,
    col.data=matrix(cols, nrow=length(cols), byrow=FALSE, ncol=5),
)

### Show how to manually tune the character size
data(iris)
reg <- lm( Sepal.Length ~ Species, data=iris )
text <- capture.output(summary(reg))

# do the plot and capture the character size used
textplot(text, valign="top")

# see what size was used
cex

# now redo the plot at 80% size
textplot( text, valign="top", cex=cex*0.80)


library(xtable)
data(tli)
 ## Demonstrate data.frame
 tli.table <- xtable(tli[1:10,])
 digits(tli.table)[c(2,6)] <- 0
 print.xtable(file="test.tex", tli.table,floating=FALSE)

grid.newpage()         
pushViewport(plotViewport(c(5, 4, 2, 2), name="area"))
pushViewport(dataViewport(pressure$temperature,
         pressure$pressure,
         name="plotRegion"))
grid.points(pressure$temperature, pressure$pressure,
     name="dataSymbols")
grid.rect(gp=gpar(fill="transparent"))
grid.lines(c(0,1), c(1,1), gp=gpar(col="white"), name="top")
grid.xaxis()
grid.yaxis()
grid.text("temperature", y=unit(-3, "lines"))
grid.text("pressure", x=unit(-3, "lines"), rot=90)
grid.edit("dataSymbols", pch=2)
upViewport(2)
grid.rect(gp=gpar(lty="dashed"))
downViewport("plotRegion")
grid.text("Pressure (mm Hg)\nversus\nTemperature (Celsius)",
    x=unit(150, "native"), y=unit(600, "native"))

b <- xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width | Species,
    data = iris, scales = "free", layout = c(2, 2),
    auto.key = list(x = .6, y = .7, corner = c(0, 0)))


